//
//  HTNewFeatureViewController.swift
//  mySinaBlog
//
//  Created by whtapple on 3/25/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit
import SnapKit
private let reuseIdentifier = "Cell"

class HTNewFeatureViewController: UICollectionViewController {
    //初始化。在创建时带上参数UICollectionViewFlowLayout，否则会崩溃
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    init(){
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(HTNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        prepareUI()

    }
    //MARK: -布局
    private func prepareUI(){
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
    }

    // MARK: -DataSource代理方法

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pageCounts
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HTNewFeatureCell
    
       cell.index = indexPath.item
    
        return cell
    }
//MARK: -滚动完后执行动画
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        if page == pageCounts - 1 {
            let index = NSIndexPath(forItem: page, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(index) as! HTNewFeatureCell
            cell.doAnimation()
        }
    }
}

//MARK: -自定义 cell
class HTNewFeatureCell: UICollectionViewCell {
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //初始化的时候进行配置
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    ///初始化方法
    private func prepareUI(){
        //添加子控件
        addSubview(bgimageView)
        addSubview(btn)
        //约束
        bgimageView.translatesAutoresizingMaskIntoConstraints = false
        btn.translatesAutoresizingMaskIntoConstraints = false
        bgimageView.snp_makeConstraints { (make) -> Void in
        make.edges.equalTo(contentView)
        }
        btn.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView).offset(-160)
            make.centerX.equalTo(contentView)
        }
    }
    //创建 index属性。便于后来为 image 赋值
    var index = 0{
        didSet{
            //给背景图赋值
        bgimageView.image = UIImage(named: "new_feature_\(index + 1)")
            //只要设置背景图片就隐藏 btn
            btn.hidden = true
        }
    }
    //动画
    func doAnimation(){
        btn.hidden = false
        btn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options:  UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
          self.btn.transform = CGAffineTransformIdentity
            }, completion: nil)
        
    }
    //按钮点击事件
    func didClickbtn() {
        AppDelegate.delegateChangeController(HTmainViewController())
    }
    //背景图片属性
  private lazy var bgimageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    //创建 button
  private lazy var btn: UIButton = {
        let button: UIButton = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.addTarget(self, action: "didClickbtn", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        
    }()
}
